use std::process;
use colored::*;

use mymod::timespent::SPENT;
use mymod::conf;
use clap::{App, Arg};

mod mymod;

fn main() {
    SPENT.with(|f| {
        f.borrow_mut().start();
    });

    //windows下开启virtual_terminal以支持终端颜色
    #[cfg(windows)]
        match control::set_virtual_terminal(true) {
        Err(_) => {
            eprintln!("windows set color error");
            process::exit(1);
        }
        Ok(()) => {}
    }

    let matches = App::new("util")
        .version("0.0.1")
        .author("lihaofeng skyline661@163.com")
        .about("a little tools to seach")
        .arg(Arg::with_name("file").long("file").short("f").required(true)
            .help("File to search.").empty_values(false))

        .arg(Arg::with_name("keywords").long("keyword").short("k").required(true)
            .help("search keywords").empty_values(false))

        .arg(Arg::with_name("ignore_case").long("ignore_case").short("ic").required(false)
            .help("ignore keywords case_insensitive").empty_values(true))
        .get_matches();

    let mut args: Vec<String> = Vec::new();

    if let Some(keywords) = matches.value_of("keywords") {
        args.push(keywords.to_string())
    }
    if let Some(file) = matches.value_of("file") {
        args.push(file.to_string())
    }
    let i = matches.occurrences_of("ignore_case");
    args.push((i >= 1).to_string());

    println!("{:?}", args);

    /*let args: Vec<String> = env::args().collect();*/

    let config = conf::Config::parse(&args).unwrap_or_else(|err| {
        eprintln!("Application error: {}", err);
        process::exit(1);
    });

    if let Err(e) = conf::search(config) {
        eprintln!("Application error: read file error({})", e.to_string());
        process::exit(1);
    }

    SPENT.with(|f| {
        let spent_time = f.borrow_mut().end();
        eprintln!("{},{}: {}{}", "search over!".green(), "use".green(), spent_time.to_string().red(), "mills".green())
    });
}




